Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.3, n = 499)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.90272 12.90565 12.90858 12.91150 12.91440 12.91727 12.92011 12.92289
##   [9] 12.92562 12.92828 12.93086 12.93336 12.93577 12.93807 12.94025 12.94231
##  [17] 12.94424 12.94603 12.94767 12.94914 12.95044 12.95157 12.95254 12.95338
##  [25] 12.95410 12.95471 12.95521 12.95561 12.95591 12.95611 12.95623 12.95626
##  [33] 12.95621 12.95609 12.95591 12.95566 12.95535 12.95499 12.95458 12.95414
##  [41] 12.95365 12.95313 12.95259 12.95202 12.95144 12.95085 12.95025 12.94965
##  [49] 12.94906 12.94847 12.94781 12.94699 12.94601 12.94490 12.94366 12.94230
##  [57] 12.94083 12.93926 12.93760 12.93585 12.93404 12.93217 12.93024 12.92827
##  [65] 12.92628 12.92426 12.92223 12.92019 12.91817 12.91616 12.91418 12.91224
##  [73] 12.91035 12.90851 12.90674 12.90505 12.90344 12.90194 12.90001 12.89718
##  [81] 12.89351 12.88909 12.88396 12.87820 12.87188 12.86506 12.85781 12.85020
##  [89] 12.84229 12.83416 12.82586 12.81747 12.80906 12.80069 12.79243 12.78434
##  [97] 12.77649 12.76896 12.76181 12.75509 12.74890 12.74328 12.73831 12.73405
## [105] 12.73057 12.72795 12.72555 12.72275 12.71961 12.71617 12.71250 12.70864
## [113] 12.70466 12.70060 12.69652 12.69248 12.68853 12.68471 12.68110 12.67774
## [121] 12.67468 12.67198 12.66970 12.66788 12.66659 12.66587 12.66579 12.66639
## [129] 12.66773 12.66987 12.67285 12.67674 12.68158 12.68812 12.69684 12.70744
## [137] 12.71962 12.73306 12.74745 12.76250 12.77788 12.79329 12.80843 12.82298
## [145] 12.83664 12.84910 12.86005 12.87092 12.88324 12.89689 12.91171 12.92757
## [153] 12.94433 12.96185 12.97998 12.99859 13.01754 13.03668 13.05588 13.07500
## [161] 13.09389 13.11241 13.13043 13.14780 13.16439 13.18006 13.19465 13.20804
## [169] 13.22009 13.23064 13.24131 13.25365 13.26748 13.28261 13.29884 13.31600
## [177] 13.33389 13.35232 13.37110 13.39005 13.40898 13.42770 13.44601 13.46374
## [185] 13.48069 13.49667 13.51150 13.52498 13.53693 13.54716 13.55547 13.56169
## [193] 13.56562 13.56708 13.56586 13.56180 13.55496 13.54573 13.53438 13.52118
## [201] 13.50639 13.49028 13.47311 13.45516 13.43670 13.41798 13.39928 13.38087
## [209] 13.36301 13.34596 13.32776 13.30643 13.28220 13.25535 13.22611 13.19475
## [217] 13.16152 13.12667 13.09045 13.05313 13.01494 12.97615 12.93702 12.89778
## [225] 12.85870 12.82003 12.78203 12.74494 12.70903 12.67454 12.64172 12.61084
## [233] 12.58214 12.55252 12.51918 12.48279 12.44408 12.40372 12.36241 12.32086
## [241] 12.27976 12.23980 12.20169 12.16612 12.13378 12.10537 12.08159 12.06101
## [249] 12.04168 12.02354 12.00652 11.99054 11.97553 11.96143 11.94816 11.93566
## [257] 11.92385 11.91266 11.90202 11.89187 11.88213 11.87380 11.86783 11.86403
## [265] 11.86223 11.86224 11.86388 11.86698 11.87134 11.87680 11.88318 11.89029
## [273] 11.89795 11.90598 11.91421 11.92246 11.93053 11.93826 11.94547 11.95196
## [281] 11.95758 11.96212 11.96542 11.96730 11.96757 11.96605 11.96257 11.95922
## [289] 11.95794 11.95838 11.96014 11.96285 11.96613 11.96959 11.97287 11.97558
## [297] 11.97734 11.97778 11.97651 11.97315 11.96734 11.95897 11.94841 11.93596
## [305] 11.92192 11.90658 11.89023 11.87317 11.85570 11.83811 11.82070 11.80377
## [313] 11.78760 11.77251 11.75877 11.74423 11.72671 11.70653 11.68400 11.65943
## [321] 11.63312 11.60540 11.57656 11.54693 11.51680 11.48649 11.45631 11.42657
## [329] 11.39759 11.36966 11.34310 11.31822 11.29534 11.27475 11.25678 11.24172
## [337] 11.22990 11.22162 11.21519 11.20883 11.20269 11.19695 11.19175 11.18726
## [345] 11.18365 11.18108 11.17970 11.17968 11.18119 11.18438 11.18941 11.19646
## [353] 11.20618 11.21891 11.23430 11.25199 11.27162 11.29286 11.31534 11.33871
## [361] 11.36262 11.38672 11.41064 11.43405 11.45659 11.47789 11.49980 11.52424
## [369] 11.55104 11.58002 11.61101 11.64382 11.67828 11.71422 11.75145 11.78980
## [377] 11.82909 11.86914 11.90979 11.95084 11.99212 12.03346 12.07468 12.11560
## [385] 12.15605 12.19584 12.23480 12.27276 12.30953 12.34493 12.37880 12.41096
## [393] 12.44122 12.46941 12.49834 12.53049 12.56522 12.60187 12.63978 12.67829
## [401] 12.71675 12.75451 12.79091 12.82529 12.85699 12.88537 12.90977 12.92953
## [409] 12.94632 12.96223 12.97722 12.99127 13.00433 13.01639 13.02739 13.03732
## [417] 13.04614 13.05381 13.06030 13.06558 13.06962 13.07238 13.07312 13.07125
## [425] 13.06693 13.06034 13.05165 13.04102 13.02863 13.01465 12.99924 12.98259
## [433] 12.96485 12.94621 12.92682 12.90687 12.88651 12.86593 12.84529 12.82476
## [441] 12.80452 12.78473 12.76556 12.74719 12.72979 12.71352 12.69856 12.68507
## [449] 12.67175 12.65728 12.64185 12.62565 12.60886 12.59167 12.57427 12.55684
## [457] 12.53956 12.52263 12.50623 12.49054 12.47575 12.46205 12.44871 12.43494
## [465] 12.42081 12.40642 12.39183 12.37715 12.36244 12.34779 12.33328 12.31899
## [473] 12.30502 12.29143 12.27830 12.26574 12.25358 12.24164 12.22992 12.21840
## [481] 12.20709 12.19597 12.18505 12.17433 12.16379 12.15343 12.14325 12.13325
## [489] 12.12342 12.11375 12.10425 12.09491 12.08572 12.07668 12.06779 12.05904
## [497] 12.05043 12.04195 12.03360
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.3, n = 499)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.55496 12.55394 12.55297 12.55205 12.55119 12.55037 12.54959 12.54886
##   [9] 12.54816 12.54749 12.54686 12.54626 12.54568 12.54512 12.54459 12.54407
##  [17] 12.54356 12.54307 12.54258 12.54209 12.54161 12.54113 12.54064 12.54015
##  [25] 12.53965 12.53916 12.53867 12.53818 12.53770 12.53723 12.53677 12.53633
##  [33] 12.53590 12.53549 12.53510 12.53473 12.53439 12.53408 12.53379 12.53354
##  [41] 12.53332 12.53314 12.53300 12.53290 12.53284 12.53282 12.53286 12.53294
##  [49] 12.53308 12.53327 12.53343 12.53351 12.53350 12.53342 12.53328 12.53310
##  [57] 12.53287 12.53262 12.53236 12.53209 12.53183 12.53158 12.53137 12.53120
##  [65] 12.53108 12.53102 12.53104 12.53115 12.53135 12.53166 12.53208 12.53264
##  [73] 12.53335 12.53420 12.53522 12.53641 12.53779 12.53937 12.54097 12.54241
##  [81] 12.54371 12.54489 12.54596 12.54693 12.54783 12.54867 12.54946 12.55023
##  [89] 12.55098 12.55174 12.55252 12.55333 12.55419 12.55513 12.55614 12.55726
##  [97] 12.55849 12.55986 12.56137 12.56305 12.56491 12.56696 12.56923 12.57173
## [105] 12.57447 12.57747 12.57975 12.58043 12.57970 12.57771 12.57464 12.57067
## [113] 12.56595 12.56068 12.55501 12.54912 12.54318 12.53736 12.53184 12.52678
## [121] 12.52236 12.51875 12.51612 12.51464 12.51449 12.51583 12.51884 12.52369
## [129] 12.53089 12.54058 12.55242 12.56606 12.58115 12.59734 12.61428 12.63163
## [137] 12.64902 12.66612 12.68257 12.69802 12.71212 12.72453 12.73718 12.75213
## [145] 12.76919 12.78815 12.80883 12.83105 12.85460 12.87930 12.90495 12.93137
## [153] 12.95835 12.98572 13.01328 13.04084 13.06820 13.09517 13.12157 13.14721
## [161] 13.17188 13.19540 13.21758 13.23823 13.25715 13.27415 13.28905 13.30165
## [169] 13.31382 13.32742 13.34221 13.35799 13.37452 13.39158 13.40894 13.42639
## [177] 13.44369 13.46063 13.47697 13.49250 13.50699 13.52021 13.53195 13.54198
## [185] 13.55007 13.55599 13.55954 13.56047 13.55858 13.55392 13.54688 13.53767
## [193] 13.52652 13.51363 13.49924 13.48355 13.46679 13.44917 13.43092 13.41226
## [201] 13.39339 13.37455 13.35595 13.33782 13.32036 13.30174 13.28012 13.25573
## [209] 13.22879 13.19953 13.16816 13.13491 13.10000 13.06366 13.02611 12.98758
## [217] 12.94828 12.90843 12.86827 12.82802 12.78789 12.74812 12.70892 12.67052
## [225] 12.63314 12.59701 12.56234 12.52936 12.49830 12.46938 12.44282 12.41567
## [233] 12.38525 12.35222 12.31723 12.28091 12.24393 12.20692 12.17053 12.13542
## [241] 12.10222 12.07159 12.04418 12.02062 12.00158 11.98586 11.97177 11.95918
## [249] 11.94796 11.93799 11.92915 11.92132 11.91435 11.90813 11.90254 11.89745
## [257] 11.89273 11.88826 11.88391 11.88139 11.88229 11.88632 11.89317 11.90254
## [265] 11.91413 11.92765 11.94280 11.95927 11.97677 11.99500 12.01366 12.03244
## [273] 12.05105 12.06920 12.08657 12.10288 12.11781 12.13108 12.14239 12.15142
## [281] 12.15789 12.16150 12.16523 12.17190 12.18092 12.19173 12.20374 12.21639
## [289] 12.22910 12.24130 12.25241 12.26186 12.26907 12.27347 12.27449 12.27155
## [297] 12.26485 12.25525 12.24311 12.22884 12.21280 12.19539 12.17698 12.15797
## [305] 12.13872 12.11962 12.10107 12.08343 12.06709 12.05244 12.03599 12.01453
## [313] 11.98888 11.95985 11.92824 11.89488 11.86057 11.82612 11.79234 11.76006
## [321] 11.73007 11.70319 11.68024 11.66201 11.64509 11.62582 11.60467 11.58213
## [329] 11.55866 11.53475 11.51086 11.48747 11.46507 11.44412 11.42510 11.40849
## [337] 11.39475 11.38438 11.37532 11.36541 11.35496 11.34426 11.33362 11.32333
## [345] 11.31369 11.30500 11.29757 11.29168 11.28765 11.28577 11.28634 11.28966
## [353] 11.29563 11.30378 11.31393 11.32587 11.33939 11.35429 11.37037 11.38742
## [361] 11.40525 11.42364 11.44240 11.46132 11.48020 11.49883 11.51872 11.54138
## [369] 11.56660 11.59420 11.62396 11.65570 11.68922 11.72431 11.76078 11.79844
## [377] 11.83707 11.87650 11.91650 11.95690 11.99749 12.03807 12.07845 12.11842
## [385] 12.15779 12.19636 12.23393 12.27031 12.30530 12.33869 12.37029 12.39990
## [393] 12.42733 12.45237 12.47740 12.50460 12.53354 12.56378 12.59488 12.62639
## [401] 12.65789 12.68892 12.71904 12.74782 12.77481 12.79957 12.82167 12.84066
## [409] 12.85780 12.87460 12.89097 12.90681 12.92206 12.93662 12.95040 12.96333
## [417] 12.97532 12.98628 12.99612 13.00476 13.01212 13.01811 13.02267 13.02580
## [425] 13.02758 13.02806 13.02730 13.02535 13.02227 13.01812 13.01295 13.00682
## [433] 12.99979 12.99192 12.98326 12.97386 12.96380 12.95311 12.94187 12.93012
## [441] 12.91793 12.90535 12.89243 12.87924 12.86583 12.85226 12.83858 12.82486
## [449] 12.81078 12.79602 12.78058 12.76449 12.74776 12.73041 12.71244 12.69389
## [457] 12.67475 12.65506 12.63481 12.61404 12.59276 12.57097 12.54864 12.52569
## [465] 12.50214 12.47799 12.45324 12.42790 12.40197 12.37546 12.34838 12.32071
## [473] 12.29248 12.26368 12.23433 12.20441 12.17394 12.14289 12.11126 12.07906
## [481] 12.04629 12.01293 11.97900 11.94449 11.90941 11.87374 11.83750 11.80067
## [489] 11.76327 11.72528 11.68671 11.64756 11.60782 11.56750 11.52660 11.48511
## [497] 11.44304 11.40038 11.35713
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.3, n = 499)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.78432 11.79155 11.79877 11.80599 11.81317 11.82030 11.82738 11.83438
##   [9] 11.84129 11.84809 11.85477 11.86131 11.86770 11.87393 11.87997 11.88581
##  [17] 11.89144 11.89684 11.90200 11.90690 11.91153 11.91586 11.91989 11.92360
##  [25] 11.92698 11.93001 11.93267 11.93494 11.93683 11.93835 11.93955 11.94045
##  [33] 11.94107 11.94142 11.94152 11.94139 11.94103 11.94047 11.93973 11.93880
##  [41] 11.93773 11.93651 11.93516 11.93371 11.93216 11.93053 11.92884 11.92710
##  [49] 11.92534 11.92356 11.92177 11.92001 11.91828 11.91659 11.91497 11.91343
##  [57] 11.91198 11.91014 11.90743 11.90392 11.89967 11.89475 11.88922 11.88314
##  [65] 11.87657 11.86958 11.86223 11.85458 11.84670 11.83865 11.83049 11.82228
##  [73] 11.81410 11.80599 11.79803 11.79027 11.78279 11.77563 11.76888 11.76258
##  [81] 11.75680 11.75161 11.74706 11.74323 11.74016 11.73655 11.73112 11.72403
##  [89] 11.71544 11.70550 11.69438 11.68221 11.66918 11.65542 11.64110 11.62637
##  [97] 11.61139 11.59631 11.58130 11.56651 11.55209 11.53820 11.52500 11.51265
## [105] 11.50129 11.49109 11.48221 11.47479 11.46901 11.46500 11.46294 11.46297
## [113] 11.46525 11.46976 11.47625 11.48458 11.49459 11.50613 11.51904 11.53319
## [121] 11.54840 11.56454 11.58145 11.59897 11.61696 11.63527 11.65373 11.67220
## [129] 11.69052 11.70855 11.72613 11.74311 11.75933 11.77774 11.80087 11.82801
## [137] 11.85843 11.89140 11.92620 11.96211 11.99841 12.03436 12.06924 12.10233
## [145] 12.13290 12.16024 12.18360 12.20578 12.22987 12.25568 12.28300 12.31163
## [153] 12.34137 12.37200 12.40332 12.43514 12.46723 12.49941 12.53146 12.56318
## [161] 12.59437 12.62482 12.65432 12.68267 12.70967 12.73512 12.75880 12.78051
## [169] 12.80005 12.81721 12.83330 12.84968 12.86628 12.88299 12.89973 12.91641
## [177] 12.93293 12.94922 12.96517 12.98071 12.99573 13.01015 13.02387 13.03682
## [185] 13.04890 13.06001 13.07007 13.07900 13.08668 13.09305 13.09801 13.10147
## [193] 13.10333 13.10351 13.10193 13.09848 13.09354 13.08756 13.08054 13.07247
## [201] 13.06336 13.05321 13.04202 13.02979 13.01653 13.00223 12.98690 12.97053
## [209] 12.95314 12.93471 12.91353 12.88809 12.85877 12.82595 12.79000 12.75129
## [217] 12.71018 12.66707 12.62231 12.57628 12.52935 12.48189 12.43429 12.38690
## [225] 12.34010 12.29427 12.24977 12.20699 12.16628 12.12803 12.09260 12.06038
## [233] 12.03172 12.00274 11.96977 11.93359 11.89495 11.85462 11.81336 11.77192
## [241] 11.73107 11.69158 11.65420 11.61969 11.58882 11.56234 11.54103 11.52326
## [249] 11.50688 11.49177 11.47785 11.46502 11.45319 11.44225 11.43211 11.42267
## [257] 11.41384 11.40552 11.39762 11.39004 11.38268 11.37746 11.37612 11.37836
## [265] 11.38388 11.39236 11.40350 11.41699 11.43252 11.44978 11.46845 11.48825
## [273] 11.50884 11.52993 11.55121 11.57236 11.59308 11.61306 11.63200 11.64957
## [281] 11.66548 11.67941 11.69107 11.70012 11.70628 11.70923 11.70865 11.70600
## [289] 11.70288 11.69928 11.69515 11.69048 11.68522 11.67936 11.67286 11.66569
## [297] 11.65783 11.64923 11.63988 11.62975 11.61880 11.60315 11.57979 11.54996
## [305] 11.51490 11.47587 11.43412 11.39088 11.34741 11.30497 11.26478 11.22811
## [313] 11.19620 11.17029 11.15164 11.13671 11.12120 11.10524 11.08894 11.07243
## [321] 11.05582 11.03924 11.02280 11.00662 10.99083 10.97554 10.96087 10.94694
## [329] 10.93388 10.92179 10.91081 10.90105 10.89263 10.88567 10.88029 10.87661
## [337] 10.87474 10.87482 10.87687 10.88072 10.88622 10.89319 10.90148 10.91090
## [345] 10.92131 10.93253 10.94439 10.95674 10.96940 10.98220 10.99499 11.00759
## [353] 11.02155 11.03828 11.05740 11.07855 11.10137 11.12550 11.15055 11.17618
## [361] 11.20201 11.22768 11.25282 11.27706 11.30005 11.32141 11.34243 11.36458
## [369] 11.38779 11.41196 11.43701 11.46285 11.48940 11.51657 11.54428 11.57244
## [377] 11.60097 11.62978 11.65878 11.68789 11.71703 11.74610 11.77503 11.80372
## [385] 11.83210 11.86007 11.88756 11.91447 11.94072 11.96622 11.99089 12.01465
## [393] 12.03740 12.05907 12.08182 12.10751 12.13559 12.16551 12.19673 12.22868
## [401] 12.26083 12.29262 12.32350 12.35292 12.38032 12.40517 12.42691 12.44498
## [409] 12.46103 12.47698 12.49271 12.50811 12.52307 12.53745 12.55115 12.56404
## [417] 12.57602 12.58697 12.59676 12.60528 12.61241 12.61804 12.62188 12.62381
## [425] 12.62394 12.62240 12.61929 12.61474 12.60886 12.60177 12.59359 12.58443
## [433] 12.57442 12.56366 12.55229 12.54040 12.52813 12.51559 12.50289 12.49016
## [441] 12.47750 12.46505 12.45291 12.44120 12.43004 12.41955 12.40985 12.40104
## [449] 12.39226 12.38260 12.37220 12.36118 12.34966 12.33776 12.32562 12.31335
## [457] 12.30108 12.28893 12.27702 12.26548 12.25443 12.24400 12.23371 12.22302
## [465] 12.21199 12.20067 12.18912 12.17739 12.16554 12.15360 12.14165 12.12973
## [473] 12.11789 12.10619 12.09467 12.08340 12.07229 12.06121 12.05017 12.03915
## [481] 12.02816 12.01720 12.00626 11.99534 11.98444 11.97355 11.96268 11.95181
## [489] 11.94096 11.93011 11.91926 11.90841 11.89756 11.88671 11.87585 11.86498
## [497] 11.85410 11.84320 11.83229
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")